package demo12;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: WangPeng
 * @createTime: 2022/11/02 21:15
 * @description:
 * @version: v1.0
 */
public class Tets01 {
    public static void main(String[] args) {
        //线程不安全
        ArrayList<String> list1 = new ArrayList<>();
        //线程安全
        CopyOnWriteArrayList<String> list2 = new CopyOnWriteArrayList<>();
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 100; i++) {
            final int a = i;
            executorService.submit(new Runnable(){
                @Override
                public void run() {
                    for (int j = 0; j < 10; j++) {
                        list1.add(Thread.currentThread().getName()+"==="+a+"==="+j);
//                        list2.add(Thread.currentThread().getName()+"==="+a+"==="+j);
                    }
                }
            });
        }
        executorService.shutdown();
        while (!executorService.isTerminated()){
            System.out.println(list1);
//            System.out.println(list2);
        }
    }
}
